package com.example.gesture.Draggable

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.offset
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import kotlin.math.roundToInt

//注意：由于Modifer链式执行，此时offset必需在draggable与background前面。
//水平拖动
@Composable
fun DraggableDemo() {
    var offsetX by remember {
        mutableStateOf(0f)
    }

    Text(
        text = "drag me",
        modifier = Modifier
            .offset { IntOffset(offsetX.roundToInt(), 0) }
            .draggable(
                orientation = Orientation.Horizontal,
                state = rememberDraggableState { delta ->
                    offsetX += delta
                    delta
                }
            )
    )
}